using System;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;


public class MouseDragLine : MonoBehaviour
{
    private UIDocument uiroot;
    private VisualElement dragElement;
    private VisualElement target;

    private bool isDragging = false;
    private Vector2 startMousePosition;
    private float startWidthOrHeight;
    [Header("重要")] public DragDirection dragDirection = DragDirection.horizontal;
    public string dragLineName = "InspectorDragLine";
    public string targetName = "left";
    public int targetDef = 65;
    public Vector2Int range = new Vector2Int(10, 90);
    public bool isIncremental = true;
    void Start()
    {
        uiroot = GetComponentInParent<UIDocument>();
        target = uiroot.rootVisualElement.Q<VisualElement>(targetName);
        dragElement = uiroot.rootVisualElement.Q<VisualElement>(dragLineName);

        dragElement.RegisterCallback<PointerDownEvent>(OnPointerDown);
        dragElement.RegisterCallback<PointerUpEvent>(OnPointerUp);
        dragElement.RegisterCallback<PointerMoveEvent>(OnPointerMove);
        target.RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);

        // 确保元素可交互
        dragElement.pickingMode = PickingMode.Position;
        float defstyleSize=  PlayerPrefs.GetFloat($"{targetName}.stylesize",targetDef);
        //总是为0，uibuilder 中设置了也无效，也行是bug,所以默认设置
        if (dragDirection == DragDirection.horizontal)
        {
            target.style.width = new Length(defstyleSize, LengthUnit.Percent);
        }
        else
        {
            target.style.height = new Length(defstyleSize, LengthUnit.Percent);
        }
    }

    private void OnGeometryChanged(GeometryChangedEvent evt)
    {
        // float widthValue = left.style.width.value.value;
        // Debug.Log($"Width: {widthValue}"); // 现在应该能正确获取 65
    }

    private void OnPointerDown(PointerDownEvent evt)
    {
        isDragging = true;
        startMousePosition = evt.position; // 记录初始鼠标位置
        if (dragDirection == DragDirection.horizontal)
        {
            startWidthOrHeight = target.style.width.value.value; // 记录初始宽度
        }
        else
        {
            startWidthOrHeight = target.style.height.value.value; // 记录初始宽度
        }

        dragElement.CapturePointer(evt.pointerId); // 捕获指针
    }

    private void OnPointerUp(PointerUpEvent evt)
    {
        isDragging = false;
        dragElement.ReleasePointer(evt.pointerId); // 释放指针
    }

    private void OnPointerMove(PointerMoveEvent evt)
    {
        if (!isDragging) return;


        if (dragDirection == DragDirection.horizontal)
        {
            // 计算鼠标移动距离（基于屏幕百分比）
            float deltaX = (evt.position.x - startMousePosition.x) / Screen.width * 100f;
            if (!isIncremental) deltaX = -deltaX;
            // 更新宽度（基于初始宽度 + 鼠标移动百分比）
            float newWidthPercent = Mathf.Clamp(startWidthOrHeight + deltaX, range.x, range.y); // 限制范围（10%~90%）
            target.style.width = new Length(newWidthPercent, LengthUnit.Percent);
            PlayerPrefs.SetFloat($"{targetName}.stylesize",newWidthPercent);
        }
        else
        {
            float deltaY = (evt.position.y - startMousePosition.y) / Screen.height * 100f;
            if (!isIncremental) deltaY = -deltaY;
            float newWidthPercent = Mathf.Clamp(startWidthOrHeight + deltaY, range.x, range.y); // 限制范围（10%~90%）
            target.style.height = new Length(newWidthPercent, LengthUnit.Percent);
            PlayerPrefs.SetFloat($"{targetName}.stylesize",newWidthPercent);
        }
    }
}